/*  
 *  BoardHash.cs 
 *  Copyright(c) 2007 Adam W Adair
 *
 *  This file is part of Adam's Chess Engine (ACE).
 *
 *  ACE is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *   
 *  ACE is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with ACE; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
using System;
using System.Collections.Generic;
using System.Text;

namespace ACE.Board
{
    /// <summary>
    /// BoardHash provides hashing functions for the chess board entities.
    /// </summary>
    class BoardHash
    {
        static int [,,]hash_piece;  
        static int hash_side;
        static int []hash_ep;
        static Random random;

        static BoardHash()
        {
            random = new Random();
            hash_piece = new int[2,6,64];
            hash_ep = new int[64];

            for (int i = 0; i < 2; ++i)
                for (int j = 0; j < 6; ++j)
                    for (int k = 0; k < 64; ++k)
                        hash_piece[i,j,k] = HashRand();
            hash_side = HashRand();
            for (int i = 0; i < 64; ++i)
                hash_ep[i] = HashRand();
        }

        public static int HashRand()
        {
            int i;
            int r = 0;

            for (i = 0; i < 32; ++i)
                r ^= random.Next() << i;
            return r;
        }

        public static int HashPiece(Piece piece, int boardSquare)
        {
            int pi = 0;
            int si = 0;
            si = (piece.Color == Piece.PlayerColor.White) ? 0 : 1;
            
            if (piece is Knight) pi = 1;
            else if (piece is Bishop) pi = 2;
            else if (piece is Rook) pi = 3;
            else if (piece is Queen) pi = 4;
            else if (piece is King) pi = 5;

            return hash_piece[si, pi, boardSquare];
        }

        public static int HashEp(int square)
        {
            return hash_ep[square];
        }

    }
}
